/**
 * Check box control.
 *
 * @author Andrey Anisimov <hercules@inbox.ru>
 * @see core.ui.RadioButton
 */

import core.ui.InputWindow;

class CheckBox extends InputWindow {

	/**
	 * Creates new check box control.
	 * 
	 * @param parent (Window) [optional] parent window
	 * @param x, y (int) [optional] coordinates
	 * @param width, height (int) [optional] control size.
	 */	
	this.CheckBox = function(parent, x, y, width, height) {
		super(parent, x, y, width, height);
		this.updateLayout();
	}
	
	/**
	 * Sets whether the check box is checked.
	 *
	 * @param checked (bool) true to check, false to uncheck
	 */
	this.setChecked = function(checked) {
		this.getInput().checked = checked;
	}
	
	/**
	 * Returns whether the check box is checked.
	 *
	 * @return bool
	 */
	this.isChecked = function() {
		return this.getInput().checked;
	}
	
	/**
	 * Sets checkbox label text.
	 *
	 * @param text (string) new text
	 */
	this.setText = function(text) {
		this.label.innerHTML = text;
	}
	
	/**
	 * Returns current checkbox lable text.
	 *
	 * @return string
	 */
	this.getText = function() {
		return this.label.innerHTML;
	}

	/**
	 * Returns input element.
	 *
	 * @return HTMLInputElement
	 */
	this.getInput = function() {
		return this.input;
	}

	this.resize = function(width, height) {
		super.resize(width, height);
		this.updateLayout();
	}
	
	this.close = function() {
		this.getElement().removeChild(this.input);
		this.getElement().removeChild(this.label);
		delete this.input;
		delete this.label;
		super.close();
	}

	/**
	 * @protected
	 * Returns check box input type (checkbox).
	 * @return string
	 */	
	this.getType = function() {
		return 'checkbox';
	}

	/**
	 * @protected
	 * Calls to recalculate check box layout.
	 */
	this.updateLayout = function() {
		this.label.style.height = this.getClientHeight() + 'px';
		this.label.style.lineHeight = this.getClientHeight() + 'px';
	}
	
	/**
	 * @protected
	 * Creates root DOM-element for check box.
	 * @return HTMLElement
	 */
	this.createElement = function() {
		var div = document.createElement('div');
		
		// Create input element
		this.input = super.createElement();
		this.input.id = this.getId() + '_input';
		
		// Adjust checkbox style to make it display almost
		// identical in IE, Mozilla and Opera.
		this.input.style.left = '0px';
		this.input.style.top = '0px';
		this.input.style.height = '100%';
		this.input.style.lineHeight = '100%';
		this.input.style.marginTop = this.input.style.marginBottom = '0px';
		this.input.style.marginRight = '3px';
		this.input.style.styleFloat = this.input.style.cssFloat = 'left';
		
		div.appendChild(this.input);
		
		// Create label element
		this.label = document.createElement('label');
		this.label.htmlFor = this.input.id;
		this.label.style.display = 'block';
		//core.ui.quirks.TextSelection.preventSelection(this.label);
		div.appendChild(this.label);
		
		return div;
	}
}